package com.ttc.ssm.controller.admin;

import com.ttc.ssm.common.CompanyPage;
import com.ttc.ssm.po.Ad;
import com.ttc.ssm.po.Customer;
import com.ttc.ssm.service.admin.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * Created by 58296 on 2016/8/1.
 */
@Controller
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    @RequestMapping("index")
    public String index(Model model) throws Exception
    {
        return "admin/ad/index";
    }

    @RequestMapping("/getAdsList")
    @ResponseBody
    public Map getAdsList(@RequestParam("p") Integer page, @RequestParam("company_name") String company_name) throws Exception
    {
        CompanyPage companyPage = new CompanyPage();
        companyPage.setCurrentPage(page);
        companyPage.setPageSize(20);
        if (company_name != null){
            companyPage.setCompany_name(company_name);
        }
        companyPage.setTotalRows(adService.countRows(companyPage));
        List<Ad> adList= adService.selectAdForPage(companyPage);
        Map map = new HashMap();
        map.put("show",companyPage);
        map.put("data", adList);
        System.out.println(adList);
        return map;
    }

    @RequestMapping("deleteAds")
    @ResponseBody
    public int deleteAds(@RequestBody Map ids) throws  Exception
    {
        try {
            adService.deleteAds(ids);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("/updateAdStatus")
    @ResponseBody
    public int updateAdStatus(@RequestParam("ad_id") int ad_id,@RequestParam("status") int status) throws Exception
    {
        try {
            adService.updateAdStatus(ad_id,status);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("edit")
    public String edit(Model model, @RequestParam("ad_id") int ad_id) throws Exception
    {
        Ad ad = adService.selectAdById(ad_id);
        model.addAttribute("ad", ad);
        return "admin/ad/edit";
    }

    @RequestMapping("/saveAd")
    @ResponseBody
    public Object saveAd(HttpServletRequest request, Ad ad, @RequestParam(value = "input01") MultipartFile temp_pic) throws Exception
    {
        try{
            if(ad.getCompany_name()==null || ad.getAd_url()==null
                    || ad.getAd_desc()==null)
                return -1;

            String imgName = saveImgFile(request,temp_pic);
            if (imgName != null) {
                ad.setAd_imgurl(imgName);
            }
            System.out.println(ad.getAd_desc()+" "+ad.getCompany_name());
            adService.updateAd(ad);
            return 1;
        }catch (Exception e){

            return 0;
        }
    }

    @RequestMapping("create")
    public String create(Model model) throws Exception
    {
        return "admin/ad/create";
    }

    @RequestMapping("/insertAd")
    @ResponseBody
    public int insertAd(HttpServletRequest request, Ad ad, @RequestParam(value = "input01") MultipartFile temp_pic) throws Exception
    {
        try{
            if(ad.getCompany_name()==null || ad.getAd_url()==null
                    || ad.getAd_desc()==null)
                return -1;

            String imgName = saveImgFile(request,temp_pic);
            if (imgName != null) {
                ad.setAd_imgurl(imgName);
            }
            System.out.println(ad.getAd_desc()+" "+ad.getCompany_name());
            adService.insertAd(ad);
            return 1;
        }catch (Exception e){

            return 0;
        }
    }

    private String saveImgFile(HttpServletRequest request, MultipartFile temp_pic) throws Exception
    {
        String originalFilename = temp_pic.getOriginalFilename();
        if(temp_pic !=null && originalFilename !=null && originalFilename.length()>0) {
            //存储图片的物理路径
            String pic_path=request.getSession().getServletContext().getRealPath("upload/ad/");
            //新的图片名称
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            System.out.println(pic_path + newFileName);
            //新图片
            File newFile = new File(pic_path + newFileName);

            //将内存的数据写入磁盘
            temp_pic.transferTo(newFile);
            return newFileName;
        }
        return null;
    }
}
